
package com.sxl.controller.admin;

import com.sxl.controller.MyController;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("ExpertController")
@RequestMapping(value = "/admin/expert")
public class ExpertController extends MyController {


    @RequestMapping(value = "/frame")
    public String frame(Model model, HttpServletRequest request, String flag) {

        return "/admin/expert/frame";
    }

    @RequestMapping(value = "/list")
    public String list(Model model, HttpServletRequest request,boolean isAdmin) {
        String sql = "select * from t_expert";
        String listString = "/admin/expert/adminList";
        if (!isAdmin) {
            Map customerBean = (Map) request.getSession().getAttribute("customerBean");
            Integer expertId = (Integer) customerBean.get("id");
            sql = "select * from t_expert where expertId=" + expertId;
            listString = "/admin/expert/list";
        }
        List list = db.queryForList(sql);
        request.setAttribute("list", list);
        return listString;
    }

    @RequestMapping(value = "/editSave")
    public ResponseEntity<String> editSave(Model model, HttpServletRequest request, Long id, String flag
            , String expertName, String picture1, String picture2, String content, String status) {
        Map customerBean = (Map) request.getSession().getAttribute("customerBean");
        Integer expertId = (Integer) customerBean.get("id");
        int result;
        if (id != null) {
            String sql = "update t_expert set expertName=?,picture1=?,picture2=?,content=?,status=?,expertId=? where id=?";
            result = db.update(sql, new Object[]{expertName, picture1, picture2, content, status, expertId, id});
        } else {
            status = "未认证";
            String sql = "insert into t_expert(expertName,picture1,picture2,content,status,expertId) values(?,?,?,?,?,?)";
            result = db.update(sql, new Object[]{expertName, picture1, picture2, content, status, expertId});
        }
        if (result == 1) {
            return renderData(true, "操作成功", null);
        } else {
            return renderData(false, "操作失败", null);
        }
    }

    @RequestMapping(value = "/edit")
    public String edit(Model model, HttpServletRequest request, Long id, String flag) throws Exception {
        Map map = new HashMap();
        if (id != null) {
            //修改
            String sql = "select * from t_expert where id=?";
            map = db.queryForMap(sql, new Object[]{id});
        } else {
            map.put("status", "未认证");
        }
        model.addAttribute("map", map);
        return "/admin/expert/edit";
    }

    @RequestMapping(value = "/approve")
    public ResponseEntity<String> approve(Model model, HttpServletRequest request, Long id, String flag) {
        String sql = "update t_expert set status=? where id=?";
        int result = db.update(sql, new Object[]{"已认证", id});
        if (result == 1) {
            return renderData(true, "操作成功", null);
        } else {
            return renderData(false, "操作失败", null);
        }

    }

    @RequestMapping(value = "/editDelete")
    public ResponseEntity<String> editDelete(Model model, HttpServletRequest request, Long id, String flag) throws Exception {
        String sql = "delete from t_expert where id=?";
        int result = db.update(sql, new Object[]{id});
        if (result == 1) {
            return renderData(true, "操作成功", null);
        } else {
            return renderData(false, "操作失败", null);
        }

    }
}
